﻿Imports System.Data.Linq
Imports System.Linq

Public Class MDrivers
    Private db As New TanNamChinhDataContext

    Public Sub New()

    End Sub

    Public Sub New(ByVal dataContext As TanNamChinhDataContext)
        db = dataContext
    End Sub

    Public Function getDriversList1() As Table(Of tblDriver)
        Try
            Return db.tblDrivers
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function getDriversList2() As IQueryable
        Try
            Dim query = From b In db.tblDrivers
                        Where b.IsActive = True
                        Order By b.ID_Driver Descending
                        Select b 'New With {b.ID_Driver, b.DriverName, b.PhoneNo, b.Address, b.ID_Manager}
            'Return (From r In db.tblCurrencies
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getDriversList2(ByVal driverName As String) As IQueryable
        Try
            Dim query = From b In db.tblDrivers
                        Where b.IsActive = True And b.DriverName.Contains(driverName)
                        Select b 'New With {b.ID_Driver, b.DriverName, b.PhoneNo, b.Address, b.ID_Manager}
            'Return (From r In db.tblCurrencies
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getDriverName(ByVal id As Integer) As String
        Dim name As String = ""
        Try
            name = (From dri In db.tblDrivers
                   Where dri.ID_Driver = id
                   Order By dri.DriverName
                   Select dri.DriverName).First
        Catch ex As Exception
            Throw ex
        End Try
        Return name
    End Function
    Public Sub InsertNewRecord(ByVal obj As tblDriver)
        'db.tblCurrencies.InsertOnSubmit(obj)
        db.GetTable(Of tblDriver)().InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub

    Public Sub UpdateRecord(ByVal obj As tblDriver)
        Dim getData = (From b In db.tblDrivers
                                Where b.ID_Driver = obj.ID_Driver
                                Select b).Single()
        getData.DriverName = obj.DriverName
        getData.PhoneNo = obj.PhoneNo
        getData.Address = obj.Address
        getData.ID_Manager = obj.ID_Manager
        db.SubmitChanges()

    End Sub
    Public Sub UpdateDeleteRecord(ByVal obj As tblDriver)
        Dim getData = (From b In db.tblDrivers
                                Where b.ID_Driver = obj.ID_Driver
                                Select b).Single()
        '  getData.DriverName = obj.DriverName
        '  getData.PhoneNo = obj.PhoneNo
        ' getData.Address = obj.Address
        ' getData.ID_Manager = obj.ID_Manager
        getData.IsActive = False
        db.SubmitChanges()

    End Sub
    Public Sub DeleteRecord(ByVal id As Integer)
        Dim getData = (From b In db.tblDrivers
                                Where b.ID_Driver = id
                                Select b).Single()
        db.tblDrivers.DeleteOnSubmit(getData)
        db.SubmitChanges()
    End Sub

    'Sub FillDriverCombo(ByVal combo As DataGridViewComboBoxColumn)
    '    Dim query As Object

    '    ' If My.Application.ActiveRoleId = 2 Then
    '    query = From a In db.tblDrivers
    '                   Select a.ID_Driver, a.DriverName
    '                   Order By DriverName Ascending
    '    'Else
    '    'query = From a In db.customers
    '    '            Select a.id, a.name
    '    '            Order By name Ascending
    '    'End If


    '    If Not IsNothing(query) Then
    '        With combo
    '            .DataSource = query
    '            .DisplayMember = "DriverName"
    '            .ValueMember = "ID_Driver"
    '        End With
    '    End If
    'End Sub

    ''' <summary>
    ''' HT Code: Lấy tài xế theo tên
    ''' </summary>
    ''' <param name="Name"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectRecordByName(ByVal Name As String) As tblDriver
        Dim query = db.tblDrivers.SingleOrDefault(Function(p As tblDriver) p.DriverName = Name And p.IsActive = True)
        Return query
    End Function
End Class
